﻿using System;

namespace UniRx.Operators
{
	// Token: 0x020002BA RID: 698
	internal class LastObservable<T> : OperatorObservableBase<T>
	{
		// Token: 0x06000F23 RID: 3875 RVA: 0x000467AC File Offset: 0x00044BAC
		public LastObservable(IObservable<T> source, bool useDefault) : base(source.IsRequiredSubscribeOnCurrentThread<T>())
		{
			this.source = source;
			this.useDefault = useDefault;
		}

		// Token: 0x06000F24 RID: 3876 RVA: 0x000467C8 File Offset: 0x00044BC8
		public LastObservable(IObservable<T> source, Func<T, bool> predicate, bool useDefault) : base(source.IsRequiredSubscribeOnCurrentThread<T>())
		{
			this.source = source;
			this.predicate = predicate;
			this.useDefault = useDefault;
		}

		// Token: 0x06000F25 RID: 3877 RVA: 0x000467EB File Offset: 0x00044BEB
		protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
		{
			if (this.predicate == null)
			{
				return this.source.Subscribe(new LastObservable<T>.Last(this, observer, cancel));
			}
			return this.source.Subscribe(new LastObservable<T>.Last_(this, observer, cancel));
		}

		// Token: 0x040008CF RID: 2255
		private readonly IObservable<T> source;

		// Token: 0x040008D0 RID: 2256
		private readonly bool useDefault;

		// Token: 0x040008D1 RID: 2257
		private readonly Func<T, bool> predicate;

		// Token: 0x020002BB RID: 699
		private class Last : OperatorObserverBase<T, T>
		{
			// Token: 0x06000F26 RID: 3878 RVA: 0x0004681F File Offset: 0x00044C1F
			public Last(LastObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
				this.notPublished = true;
			}

			// Token: 0x06000F27 RID: 3879 RVA: 0x00046837 File Offset: 0x00044C37
			public override void OnNext(T value)
			{
				this.notPublished = false;
				this.lastValue = value;
			}

			// Token: 0x06000F28 RID: 3880 RVA: 0x00046848 File Offset: 0x00044C48
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x06000F29 RID: 3881 RVA: 0x00046880 File Offset: 0x00044C80
			public override void OnCompleted()
			{
				if (this.parent.useDefault)
				{
					if (this.notPublished)
					{
						this.observer.OnNext(default(T));
					}
					else
					{
						this.observer.OnNext(this.lastValue);
					}
					try
					{
						this.observer.OnCompleted();
					}
					finally
					{
						base.Dispose();
					}
				}
				else if (this.notPublished)
				{
					try
					{
						this.observer.OnError(new InvalidOperationException("sequence is empty"));
					}
					finally
					{
						base.Dispose();
					}
				}
				else
				{
					this.observer.OnNext(this.lastValue);
					try
					{
						this.observer.OnCompleted();
					}
					finally
					{
						base.Dispose();
					}
				}
			}

			// Token: 0x040008D2 RID: 2258
			private readonly LastObservable<T> parent;

			// Token: 0x040008D3 RID: 2259
			private bool notPublished;

			// Token: 0x040008D4 RID: 2260
			private T lastValue;
		}

		// Token: 0x020002BC RID: 700
		private class Last_ : OperatorObserverBase<T, T>
		{
			// Token: 0x06000F2A RID: 3882 RVA: 0x0004697C File Offset: 0x00044D7C
			public Last_(LastObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
				this.notPublished = true;
			}

			// Token: 0x06000F2B RID: 3883 RVA: 0x00046994 File Offset: 0x00044D94
			public override void OnNext(T value)
			{
				bool flag;
				try
				{
					flag = this.parent.predicate(value);
				}
				catch (Exception error)
				{
					try
					{
						this.observer.OnError(error);
					}
					finally
					{
						base.Dispose();
					}
					return;
				}
				if (flag)
				{
					this.notPublished = false;
					this.lastValue = value;
				}
			}

			// Token: 0x06000F2C RID: 3884 RVA: 0x00046A08 File Offset: 0x00044E08
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x06000F2D RID: 3885 RVA: 0x00046A40 File Offset: 0x00044E40
			public override void OnCompleted()
			{
				if (this.parent.useDefault)
				{
					if (this.notPublished)
					{
						this.observer.OnNext(default(T));
					}
					else
					{
						this.observer.OnNext(this.lastValue);
					}
					try
					{
						this.observer.OnCompleted();
					}
					finally
					{
						base.Dispose();
					}
				}
				else if (this.notPublished)
				{
					try
					{
						this.observer.OnError(new InvalidOperationException("sequence is empty"));
					}
					finally
					{
						base.Dispose();
					}
				}
				else
				{
					this.observer.OnNext(this.lastValue);
					try
					{
						this.observer.OnCompleted();
					}
					finally
					{
						base.Dispose();
					}
				}
			}

			// Token: 0x040008D5 RID: 2261
			private readonly LastObservable<T> parent;

			// Token: 0x040008D6 RID: 2262
			private bool notPublished;

			// Token: 0x040008D7 RID: 2263
			private T lastValue;
		}
	}
}
